package cn.px.rest.controller.firm;

import cn.px.common.bean.AbstractPageResultBean;
import cn.px.common.util.BeanConverterUtil;
import cn.px.common.util.R;
import cn.px.common.validation.group.AddGroup;
import cn.px.common.validation.group.UpdateGroup;
import cn.px.dao.dto.firm.FirmProducerDTO;
import cn.px.dao.entity.firm.FirmProducer;
import cn.px.dao.util.ExcelUtil;
import cn.px.dao.util.SelectUtil;
import cn.px.dao.vo.export.firm.ExportFirmProducerVO;
import cn.px.dao.vo.query.firm.FirmProducerVO;
import cn.px.rest.log.annotation.SysLog;
import cn.px.service.firm.FirmProducerService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description 生产商 控制器
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2020,北京乾元坤和科技有限公司 All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("/firm/producers")
public class FirmProducerController {

    private final FirmProducerService firmProducerService;

    /**
     * 通过id查询单条记录
     *
     * @param id 主键
     * @return R
     */
    @GetMapping("/{id}")
    public R<FirmProducer> getById(@PathVariable("id") String id) {
        return new R<>(firmProducerService.getModelById(id));
    }

    /**
     * 新增记录
     *
     * @param firmProducerDTO 生产商DTO
     * @return R
     */
    @SysLog("新增生产商")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('firm-producers-add')")
    public R save(@Validated({AddGroup.class}) @RequestBody FirmProducerDTO firmProducerDTO) {
        return new R<>(firmProducerService.saveModelByDTO(firmProducerDTO));
    }

    /**
     * 修改记录
     *
     * @param firmProducerDTO 生产商DTO
     * @return R
     */
    @SysLog("修改生产商")
    @PutMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('firm-producers-edit')")
    public R update(@PathVariable("id") String id, @Validated({UpdateGroup.class}) @RequestBody FirmProducerDTO firmProducerDTO) {
        firmProducerService.modModelByDTO(firmProducerDTO);
        return new R<>();
    }

    /**
     * 通过id删除一条记录
     *
     * @param id 主键
     * @return R
     */
    @SysLog("删除生产商")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('firm-producers-del')")
    public R removeById(@PathVariable String id) {
        firmProducerService.deleteByPrimaryKey(id);
        return new R<>();
    }

    /**
     * 批量记录
     *
     * @param ids 主键
     * @return R
     */
    @SysLog("批量删除生产商")
    @DeleteMapping("")
    @PreAuthorize("@pms.hasPermission('firm-producers-del')")
    public R removeIds(@RequestBody List<String> ids) {
        firmProducerService.batchDelete(ids);
        return new R<>();
    }

    /**
     * 获取分页
     *
     * @param pageNum         当前页码
     * @param pageSize        每页条数
     * @param firmProducerDTO 生产商DTO
     * @return R
     */
    @GetMapping("/page")
    public R<AbstractPageResultBean<FirmProducerVO>> query(FirmProducerDTO firmProducerDTO, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "20") int pageSize) {
        return new R<>(firmProducerService.selectPageList(firmProducerDTO, pageNum, pageSize));
    }

    /**
     * 获取列表
     *
     * @param firmProducerDTO 生产商DTO
     * @return R
     */
    @GetMapping("")
    public R query(FirmProducerDTO firmProducerDTO) {
        return new R<>(firmProducerService.selectList(firmProducerDTO));
    }

    /**
     * 生产商导出
     *
     * @param firmProducerDTO 生产商DTO
     * @return R
     */
    @GetMapping("/export")
    @SysLog("生产商导出")
    @PreAuthorize("@pms.hasPermission('firm-producers-export')")
    public void export(HttpServletResponse response, FirmProducerDTO firmProducerDTO) {
        List<FirmProducer> list = firmProducerService.getModelListByDTO(firmProducerDTO);
        ExcelUtil.exportResponseDict(response, ExportFirmProducerVO.class, BeanConverterUtil.copyListProperties(list, ExportFirmProducerVO.class), "生产商");
    }

    /**
     * 获取下拉列表
     *
     * @param firmProducerDTO 生产商DTO
     * @return R
     */
    @GetMapping("options")
    public R queryOptions(FirmProducerDTO firmProducerDTO) {
        return new R<>(SelectUtil.buildProducerOptions(firmProducerService.getModelListByDTO(firmProducerDTO)));
    }
}
